package ext.acae2.dms.web.input;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.rmi.NotBoundException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;

import wt.log4j.LogR;
import ext.acae2.dms.bean.EPMMemberLinkBean;
import ext.acae2.dms.bean.EPMReferenceLinkBean;
import ext.acae2.util.UGHostUtil;

public class UGClient {
	
	private static final Logger LOGGER = LogR.getLogger(UGClient.class
			.getName());
	
    public static boolean execute(List<EPMMemberLinkBean> epmMemberLinkList, List<EPMReferenceLinkBean> epmReferenceLinkList, String userName, String time) throws NotBoundException, IOException, InterruptedException {    	
    	
    	long start = System.currentTimeMillis();
    	
    	List<String> strList = new ArrayList<String>();
    	boolean b = false;		
    	
		if(epmMemberLinkList != null){
			for (int i = 0; i < epmMemberLinkList.size(); i++) {
				if(epmMemberLinkList.get(i).getParentEpm().getFormat().equals("prt") && epmMemberLinkList.get(i).getChildEpm().getFormat().equals("prt")){
					String parentEPMDocPath = epmMemberLinkList.get(i).getParentEpm().getLocationRef();
					String childEPMDocPath = epmMemberLinkList.get(i).getChildEpm().getLocationRef();
					String tcNo = epmMemberLinkList.get(i).getChildEpm().getTCNo();
					
					b = changeModels(parentEPMDocPath, childEPMDocPath, tcNo, strList, userName, time);
				}
			}
		}
		
		if(epmReferenceLinkList != null){
			for (int i = 0; i < epmReferenceLinkList.size(); i++) {
				if(epmReferenceLinkList.get(i).getRefEpmBean().getFormat().equals("prt") && epmReferenceLinkList.get(i).getEpmBean().getFormat().equals("prt")){
					String childEPMDocPath = epmReferenceLinkList.get(i).getRefEpmBean().getLocationRef();
					String parentEPMDocPath = epmReferenceLinkList.get(i).getEpmBean().getLocationRef();
					String tcNo = epmReferenceLinkList.get(i).getRefEpmBean().getTCNo();
					
					b = changeModels(parentEPMDocPath, childEPMDocPath, tcNo, strList, userName, time);
				}
			}
		}
		
		b = true;
		
		LOGGER.debug("UG转模(秒)：" + (System.currentTimeMillis() - start)/1000 );
		
		return b;
    }
    
    public static boolean changeModels(String parentEPMDocPath, String childEPMDocPath, String tcNo, List<String> strList, String userName, String time) throws HttpException, IOException{
    	
    	boolean b = false;
    	
			int parentStartIndex = parentEPMDocPath.lastIndexOf(File.separator);
			int parentEndIndex = parentEPMDocPath.lastIndexOf(".");
			int childStartIndex = childEPMDocPath.lastIndexOf(File.separator);
			int childEndIndex = childEPMDocPath.lastIndexOf(".");
			
			String childEPMDocName = childEPMDocPath.substring(childStartIndex + 1, childEndIndex);
			String parentEPMDocName = parentEPMDocPath.substring(parentStartIndex + 1, parentEndIndex);
			
			if(!strList.contains(parentEPMDocName + childEPMDocName)){
				strList.add(parentEPMDocName + childEPMDocName);
			
				parentEPMDocPath = UGHostUtil.UGMODELS + userName + "\\" + time + "\\" + parentEPMDocName + ".prt";
				childEPMDocPath = UGHostUtil.UGMODELS + userName + "\\" + time + "\\" + childEPMDocName + ".prt";
			
				LOGGER.debug("parentEPMDocPath:" + parentEPMDocPath);
				LOGGER.debug("childEPMDocName:" + childEPMDocName);
				LOGGER.debug("tcNo:" + tcNo);
				LOGGER.debug("childEPMDocPath:" + childEPMDocPath);
			
				MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
				HttpClient httpClient = new HttpClient(connectionManager);
				HostConfiguration hostConfig = httpClient.getHostConfiguration();
				hostConfig.setHost(UGHostUtil.UGHOST, 8080);
			
				PostMethod postMethod = new PostMethod(
					"/UGServer/servlet/ug?className=ext.action.UGAction");
				String str = parentEPMDocPath + " " + childEPMDocName + " " + tcNo + " " + childEPMDocPath;
				str = URLEncoder.encode(str,"utf-8");
				postMethod.setRequestHeader("command", str);
//				postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,3000);
				postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
					new DefaultHttpMethodRetryHandler());
				postMethod.addRequestHeader("Content-Type","application/octet-stream;charset=utf-8");
				httpClient.executeMethod(postMethod);
			
				int status = postMethod.getStatusLine().getStatusCode();
				if (status == HttpStatus.SC_OK) {
					LOGGER.debug("sucess");
					b = true;
				}else{
					LOGGER.debug("fail");
					return false;
				}
			}
		return b;
    }
}
